/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package AFEED;

/**
 *
 * @author comiang
 */
import RunableSt.authenticator;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;


public class RSSReader  {

private static RSSReader instance = null;

private RSSReader() {
}

public static RSSReader getInstance() {
if(instance == null) {
instance = new RSSReader();
}
return instance;
}
public  void ReadFeed(String link,JTable JT){
   
        try {
         //   URL u = new URL("http://feeds2.feedburner.com/settrade/researchAll?format=xml"); // your feed u
          String buffer;
          String url = link,
               proxy = "proxy.up.ac.th",
               port = "8080",
               username = "up\\51286885",
               password = "7489250721";

          URL server = new URL(url);

            Authenticator.setDefault(new authenticator(
                        username,password));
            Properties systemProperties = System.getProperties();
            systemProperties.setProperty("http.proxyHost",proxy);
            systemProperties.setProperty("http.proxyPort",port);
            HttpURLConnection connection = (HttpURLConnection)server.openConnection();
            connection.connect();

          BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"TIS-620")) ;
            StringBuffer    content = new StringBuffer("");   
              int count2=1;boolean chkSt=false;
              ArrayList feedtitle = new ArrayList();
              ArrayList feedlink = new ArrayList();
              ArrayList feedpub = new ArrayList();
              ArrayList feedauthor = new ArrayList();
                while((buffer = in.readLine()) != null) {
                         count2++;    
                String replace = buffer.replace("<!", "").replace("[CDATA[", "").replace("]]>", "");
                
                
                            if(buffer.contains("<item>")){
                                chkSt=true;
                            }if(buffer.contains("</item>")){
                                  System.out.println();  
                                chkSt=false;
                            }
                       if(buffer.contains("<link>")&&chkSt){ 
                            String patterntitle = "(<link.*?>)(.+?)(</link>)";
                            String updated = replace.replaceAll(patterntitle, "$2");
                            //System.out.println(updated);  
                            feedlink.add(updated);
                            content.append(replace).append("\n");
                            //System.out.println(buffer);
                       }else if(buffer.contains("<title>")&&chkSt){
                            String patterntitle = "(<title.*?>)(.+?)(</title>)";
                            String updated = replace.replaceAll(patterntitle, "$2");
                           // System.out.println(updated);       
                            feedtitle.add(updated);
                            content.append(replace).append("\n");
                       }else if(buffer.contains("<pubDate>")&&chkSt){
                           String patterntitle = "(<pubDate.*?>)(.+?)(</pubDate>)";
                            String updated = replace.replaceAll(patterntitle, "$2");
                           // System.out.println(updated);  
                            feedpub.add(updated);
                           content.append(replace).append("\n");      
                       }else if(buffer.contains("<author>")&&chkSt){ 
                            String patterntitle = "(<author.*?>)(.+?)(</author>)";
                            String updated = replace.replaceAll(patterntitle, "$2");
                           // System.out.println(updated); 
                            feedauthor.add(updated);
                           content.append(replace).append("\n");
                           
                        }
                         
                    
                       
                            
                            
                        
                        
                      //S content.append(count2).append("-->").append(buffer).append("\n");
                   

                  }
           String[] Col1 = {"Title","Date","Author",""};
           DefaultTableModel model=new DefaultTableModel(Col1,0);
           
                
                for(int x=0;x<feedauthor.size();x++){
                    String ax[]={feedtitle.get(x).toString(),feedpub.get(x).toString(),feedauthor.get(x).toString(),feedlink.get(x).toString()};
                    model.addRow(ax);
                    System.out.println(feedtitle.get(x));
                    System.out.println(feedlink.get(x));
                    System.out.println(feedpub.get(x));
                    System.out.println(feedauthor.get(x));
                }
                JT.setModel(model);
                 JT.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
       
       JT.getColumnModel().getColumn(0).setResizable(false);
        JT.getColumnModel().getColumn(0).setPreferredWidth(975);
        JT.getColumnModel().getColumn(1).setResizable(false);
        JT.getColumnModel().getColumn(1).setPreferredWidth(150);
        JT.getColumnModel().getColumn(2).setResizable(true);
       JT.getColumnModel().getColumn(2).setPreferredWidth(150);  
        JT.getColumnModel().getColumn(3).setResizable(true);
       JT.getColumnModel().getColumn(3).setPreferredWidth(0);  
        // System.out.println(content);
         
        // RSSReader1 reader = RSSReader1.getInstance();
        // reader.writeNews(content.toString());
        } catch (IOException ex) {
            Logger.getLogger(RSSReader.class.getName()).log(Level.SEVERE, null, ex);
        }

}

public void writeNews() {
try {

//DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
URL u = new URL("http://feeds2.feedburner.com/settrade/researchAll?format=xml"); // your feed url

SAXBuilder builder = new SAXBuilder();
Document document = builder.build(new File("test.xml"));
//Read xml data
Element root = document.getRootElement();
String value = root.getChild("title").getText();
System.out.println(value);




//NodeList nodes = doc.getElementsByTagName("item");

//for(int i=0;i<nodes.getLength();i++) {
//
//Element element = (Element)nodes.item(i);
//System.out.println("Title: " + getElementValue(element,"title"));
//System.out.println("Link: " + getElementValue(element,"link"));
//System.out.println("Publish Date:" + getElementValue(element,"pubDate"));
////System.out.println("author: " + getElementValue(element,"dc:creator"));
////System.out.println("comments: " + getElementValue(element,"wfw:comment"));
//System.out.println("description: " + getElementValue(element,"description"));
//System.out.println();
//}//for
}//try
catch(Exception ex) {
ex.printStackTrace();
}

}



}

